use sea_orm_migration::{prelude::*, schema::*};
use crate::DeriveIden;

#[derive(DeriveIden)]
pub enum CommonField {
    Id,
    CreatedAt,
    UpdatedAt,
    DeleteAt,
}

#[derive(DeriveIden)]
pub enum Users {
    Table,
    Username,
    PasswordHash,
    Phone,
    Email,
    Token,
    Authorize
}
impl Users {
    pub fn table_columns_fields() -> Vec<ColumnDef> {
        vec![
            pk_auto(CommonField::Id),
            string(Users::Username),
            string(Users::PasswordHash),
            string(Users::Phone),
            string(Users::Email),
            string(Users::Authorize),
            string(Users::Token),
            timestamp_with_time_zone(CommonField::CreatedAt),
            timestamp_with_time_zone(CommonField::UpdatedAt),
            timestamp_with_time_zone(CommonField::DeleteAt),
        ]
    }
    pub async fn create_table(manager: &SchemaManager<'_>)->Result<(),DbErr> {
        let mut table_builder = Table::create();
        table_builder= table_builder.table(Self::Table).if_not_exists().to_owned();
        for x in Self::table_columns_fields() {
            table_builder = table_builder.col(x).to_owned();
        }
        manager.create_table(table_builder.to_owned()).await?;
        Ok(())
    }
    pub async fn delete_table(manager: &SchemaManager<'_>)->Result<(),DbErr> {
        manager.drop_table(Table::drop().table(Self::Table).to_owned())
            .await?;
        Ok(())
    }
}
#[derive(DeriveIden)]
pub enum Account {
    Table,
    Name,
    Avatar,
    Url,
    RelationId,
    Note,
    Nick,
    Cookie,
}

impl Account {
    pub fn table_columns_fields() -> Vec<ColumnDef> {
        vec![
            pk_auto(CommonField::Id),
            string(Account::Name),
            string(Account::Avatar),
            string(Account::Url),
            string(Account::RelationId),
            string(Account::Note),
            string(Account::Nick),
            string(Account::Cookie),
            timestamp_with_time_zone(CommonField::CreatedAt),
            timestamp_with_time_zone(CommonField::UpdatedAt),
            timestamp_with_time_zone(CommonField::DeleteAt),
        ]
    }
    pub async fn create_table(manager: &SchemaManager<'_>)->Result<(),DbErr> {
        let mut table_builder = Table::create();
        table_builder= table_builder.table(Self::Table).if_not_exists().to_owned();
        for x in Self::table_columns_fields() {
            table_builder = table_builder.col(x).to_owned();
        }
        manager.create_table(table_builder.to_owned()).await?;
        Ok(())
    }
    pub async fn delete_table(manager: &SchemaManager<'_>)->Result<(),DbErr> {
        manager.drop_table(Table::drop().table(Self::Table).to_owned())
            .await?;
        Ok(())
    }
}

#[derive(DeriveIden)]
pub enum Permissions {
    Table,
    Name,
}

impl Permissions {
    pub fn table_columns_fields() -> Vec<ColumnDef> {
        vec![
            pk_auto(CommonField::Id),
            string(Account::Name),
            timestamp_with_time_zone(CommonField::CreatedAt),
            timestamp_with_time_zone(CommonField::UpdatedAt),
            timestamp_with_time_zone(CommonField::DeleteAt),
        ]
    }
    pub async fn create_table(manager: &SchemaManager<'_>)->Result<(),DbErr> {
        let mut table_builder = Table::create();
        table_builder= table_builder.table(Self::Table).if_not_exists().to_owned();
        for x in Self::table_columns_fields() {
            table_builder = table_builder.col(x).to_owned();
        }
        manager.create_table(table_builder.to_owned()).await?;
        Ok(())
    }
    pub async fn delete_table(manager: &SchemaManager<'_>)->Result<(),DbErr> {
        manager.drop_table(Table::drop().table(Self::Table).to_owned())
            .await?;
        Ok(())
    }
}